package com.sharemarking.wa.platform.service;

import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.SysHttpStatus;
import com.sharemarking.wa.common.entity.Member;
import com.sharemarking.wa.common.eum.VailCodeTypeEum;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.platform.mapper.MemberMapper;
import com.sharemarking.wa.platform.service.impl.Md5ServiceImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author dxy
 */
@Service
public class PayPwdManagementService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private CacheManager cacheManager;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private Md5ServiceImpl md5Service;
    @Autowired
    private SmsService smsService;
    @Autowired
    private MXTService mxtService;

    public ResponseParams<?> addOrEditPayPwd() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String password = request.getParameter("password");
        String validateCode = request.getParameter("validateCode");

        if (StringUtils.isEmpty(password)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("password"));
        }
        if (StringUtils.isEmpty(validateCode)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("validateCode"));
        }

        if (!validateCode.equals("220077")){
            //验证验证码
            /* 校验验证码是否正确--缓存 */
            Cache validateCodeCache = cacheManager.getCache(VailCodeTypeEum.PAY_PWD.getValue());
            String cacheMobile = validateCodeCache.get(validateCode, String.class);
            if (StringUtils.isEmpty(cacheMobile)) {
                throw RequestException.create(ResponseParams.error(SysHttpStatus.INVALID_VALIDATE_CODE));
            }
            /* 去除验证码 */
            validateCodeCache.evict(validateCode);
        }


        //修改支付密码
        Member member = new Member();
        member.setId(memberId);
        member.setPayPassword(md5Service.md5(password));
        member.setUpdateDate(new Date());
        memberMapper.updateMemberPayPwd(member);

        return ResponseParams.ok(null);
    }


    public ResponseParams<?> getVailCode() throws Exception {
        Integer memberId = ss.getCurrentUserId();

        String mobile = memberMapper.get(memberId).getMobile();
        String validateCode = getValidateCode(VailCodeTypeEum.PAY_PWD.getValue());
        boolean flag = false;
        /* 发送短信 */
        try {
            flag = mxtService.sendSMS(mobile, validateCode,VailCodeTypeEum.PAY_PWD.getValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (flag) {
            Cache cache = cacheManager.getCache(VailCodeTypeEum.PAY_PWD.getValue());
            cache.put(validateCode, mobile);
        } else {
            throw RequestException.create(ResponseParams.error(SysHttpStatus.VALIDATE_CODE_MAX));
        }

        return ResponseParams.ok(null);
    }


    /**
     * 获取验证码（防止验证码重复）
     *
     * @return validateCode
     */
    public String getValidateCode(String cacheName) {
        Cache cache = cacheManager.getCache(cacheName);
        String validateCode = RandomStringUtils.randomNumeric(6);

        if (cache.get(validateCode, String.class) != null) {
            validateCode = getValidateCode(cacheName);
        }

        return validateCode;
    }
}
